package cn.smile.service.common;

import java.util.List;

import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import cn.smile.bean.PageBean;
import cn.smile.dao.common.FriendLinkDao;
import cn.smile.entity.common.FriendLink;

import javax.annotation.Resource;

/**
 * 友情链接service
 */
@Service
public class FriendLinkService {

    @Resource
    private FriendLinkDao friendLinkDao;

    /**
     * 新闻信息添加/修改（id不为空则为修改）
     */
    public FriendLink save(FriendLink friendLink) {
        return friendLinkDao.save(friendLink);
    }

    /**
     * 分页搜索友情链接
     */
    public PageBean<FriendLink> findList(PageBean<FriendLink> pageBean, FriendLink friendLink) {
        ExampleMatcher exampleMatcher = ExampleMatcher.matching();
        exampleMatcher = exampleMatcher.withMatcher("name", GenericPropertyMatchers.contains());
        exampleMatcher = exampleMatcher.withIgnorePaths("sort");
        Example<FriendLink> example = Example.of(friendLink, exampleMatcher);
        Sort sort = Sort.by(Direction.ASC, "sort");
        PageRequest pageable = PageRequest.of(pageBean.getCurrentPage() - 1, pageBean.getPageSize(), sort);
        Page<FriendLink> findAll = friendLinkDao.findAll(example, pageable);
        pageBean.setContent(findAll.getContent());
        pageBean.setTotal(findAll.getTotalElements());
        pageBean.setTotalPage(findAll.getTotalPages());
        return pageBean;
    }

    /**
     * 根据id查询
     */
    public FriendLink find(Long id) {
        return friendLinkDao.find(id);
    }

    /**
     * 友情链接删除
     */
    public void delete(Long id) {
        friendLinkDao.deleteById(id);
    }

    /**
     * 获取制定数量的新闻列表
     */
    public List<FriendLink> findList(int size) {
        FriendLink friendLink = new FriendLink();
        PageBean<FriendLink> pageBean = new PageBean<>();
        pageBean.setPageSize(size);
        return findList(pageBean, friendLink).getContent();
    }
}
